//    Copyright (C) Kherty.  All rights reserved.
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
     public class WorkbookView: IOfficeXmlSerializable, INotifyPropertyChanged
    {
         public WorkbookView(Workbook workbook)
         {
             this.workbook = workbook;
         }

         readonly Workbook workbook;

         private int _activeTab;

         internal int ActiveTab
         {
             get { return _activeTab; }
         }
	
        private WorkbookVisibility visibility;

        public WorkbookVisibility Visibility
        {
            get { return visibility; }
            set { visibility = value; }
        }
	

        private bool minimized;

        public bool Minimized
        {
            get { return minimized; }
            set { minimized = value; }
        }

        private bool showHorizontalScroll = true;

        public bool ShowHorizontalScroll
        {
            get { return showHorizontalScroll; }
            set { showHorizontalScroll = value; }
        }
        private bool showVerticalScroll = true;

        public bool ShowVerticalScroll
        {
            get { return showVerticalScroll; }
            set { showVerticalScroll = value; }
        }

        private bool _showSheetTabs = true;

        public bool ShowSheetTabs
        {
            get { return _showSheetTabs; }
            set { _showSheetTabs = value; }
        }

        //\\private int activeTab;
         Sheet _activeSheet;
         public Sheet ActiveSheet
         {
             get
             {
                 return _activeSheet;
             }
             set
             {
                 if (_activeSheet == value)
                     return;
                 if (workbook.Sheets.IndexOf(value) == -1)
                     return;
                 _activeSheet = value;
                 notifyPropertyChanged("ActiveSheet");
             }
         }


        private bool autoFilterDateGrouping;

        public bool AutoFilterDateGrouping
        {
            get { return autoFilterDateGrouping; }
            set { autoFilterDateGrouping = value; }
        }







        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
         void notifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
             if (PropertyChanged != null)
                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
         }

        #endregion

         #region IOfficeXmlSerializable Members

         void IOfficeXmlSerializable.ReadXml(ReadContext context)
         {
             using (ReadContext c = context.Read("workbookView"))
             {
                 Minimized = c.GetOptionalBoolean("minimized", false);
                 ShowHorizontalScroll = c.GetOptionalBoolean("showHorizontalScroll", true);
                 showVerticalScroll = c.GetOptionalBoolean("showVerticalScroll", true);
                 _showSheetTabs = c.GetOptionalBoolean("showSheetTabs", true);
                 //\\xWindow, yWindow windowWidth, windowHeight tabRatio firstSheet
                 _activeTab = c.GetOptionalInteger("activeTab", 0);
                 autoFilterDateGrouping = c.GetOptionalBoolean("autoFilterDateGrouping", true);
             }

         }

         void IOfficeXmlSerializable.WriteXml(WriteContext context)
         {
             using (WriteContext c = context.Write("workbookView"))
             {
                 c.SetOptionalBoolean("minimized", Minimized, false);
                 c.SetOptionalBoolean("showHorizontalScroll", ShowHorizontalScroll = true);
                 c.SetOptionalBoolean("showVerticalScroll", showVerticalScroll, true);
                 c.SetOptionalBoolean("showSheetTabs", _showSheetTabs, true);
                 //\\xWindow, yWindow windowWidth, windowHeight tabRatio firstSheet
                 int i = workbook.Sheets.IndexOf(ActiveSheet);
                 if (i != -1)
                    c.SetOptionalInteger("activeTab", i, 0);
                 c.SetOptionalBoolean("autoFilterDateGrouping", autoFilterDateGrouping, true);
             }

         }
         #endregion

         internal void ReadXml(ReadContext context)
         {
             IOfficeXmlSerializable s = this;
             s.ReadXml(context);
         }
     }
}
